<template>
  <header class="article-header">
    <!-- 文章标题 -->
    <h1 class="article-title">{{ article.title }}</h1>

    <!-- 文章元信息 -->
    <div class="article-meta">
      <!-- 作者信息 -->
      <div class="author-info" @click="handleAuthorClick">
        <img
          :src="formatAvatarUrl(article.author.avatar) || '/default-avatar.jpg'"
          :alt="article.author.nickName"
          class="author-avatar"
          @error="handleAvatarError"
        />
        <div class="author-details">
          <div class="author-name">{{ article.author.nickName }}</div>
          <div class="publish-info">
            <span class="publish-time">{{
              formatTime(article.publishTime)
            }}</span>
            <span class="reading-time"
              >· 阅读约 {{ formatReadingTime(article.readingTime) }}</span
            >
            <span class="word-count"
              >· {{ formatNumber(article.wordCount) }} 字</span
            >
          </div>
        </div>
      </div>

      <!-- 操作按钮 -->
      <div class="action-buttons">
        <el-button
          :type="interactionStatus.isFollowedAuthor ? 'default' : 'primary'"
          size="small"
          @click="$emit('follow')"
        >
          {{ interactionStatus.isFollowedAuthor ? "已关注" : "关注" }}
        </el-button>
      </div>
    </div>

    <!-- 文章标签 -->
    <div v-if="article.tags.length > 0" class="article-tags">
      <el-tag
        v-for="tag in article.tags"
        :key="tag.id"
        :color="tag.color"
        class="article-tag"
        size="small"
        style="color: aliceblue"
      >
        {{ tag.name }}
      </el-tag>
    </div>

    <!-- 文章统计和标识 -->
    <div class="article-stats">
      <div class="stats-left">
        <span class="stat-item">
          <el-icon><View /></el-icon>
          {{ formatNumber(article.viewCount) }}
        </span>
        <span class="stat-item">
          <el-icon><LinearNewOhterLikeThumbUp /></el-icon>
          {{ formatNumber(article.likeCount) }}
        </span>
        <span class="stat-item">
          <el-icon><ChatDotSquare /></el-icon>
          {{ formatNumber(article.commentCount) }}
        </span>
        <span class="stat-item">
          <el-icon><Star /></el-icon>
          {{ formatNumber(article.collectCount) }}
        </span>
      </div>

      <div class="stats-right">
        <el-tag v-if="isTruthy(article.isTop)" type="danger" size="small"
          >置顶</el-tag
        >
        <el-tag v-if="isTruthy(article.isOriginal)" type="success" size="small"
          >原创</el-tag
        >
        <el-tag v-else type="info" size="small">转载</el-tag>
        <!-- <el-tag type="info" size="small">{{ article.category.name }}</el-tag> -->
      </div>
    </div>

    <!-- 文章摘要 -->
    <div v-if="article.summary" class="article-summary">
      {{ article.summary }}
    </div>
  </header>
</template>

<script setup lang="ts">
import { useRouter } from "vue-router";
import { View, Star, ChatDotSquare, Collection } from "@element-plus/icons-vue";
import { LinearNewOhterLikeThumbUp } from "@element-extended-icon-pack/vue";
import {
  UserArticleUtils,
  type UserArticleDetailResponse,
  type UserInteractionStatus
} from "@/api/user-article";
import { formatAvatarUrl, formatImageUrl, isTruthy } from "@/utils/avatar";

defineOptions({
  name: "ArticleHeader"
});

interface Props {
  article: UserArticleDetailResponse;
  interactionStatus: UserInteractionStatus;
}

const props = defineProps<Props>();

defineEmits<{
  like: [];
  collect: [];
  follow: [];
  share: [];
}>();

const router = useRouter();

// 工具方法
const formatTime = UserArticleUtils.formatTime;
const formatNumber = UserArticleUtils.formatNumber;
const formatReadingTime = UserArticleUtils.formatReadingTime;

// 处理作者点击
const handleAuthorClick = () => {
  const authorId = props.article.author.userId;
  if (authorId) {
    console.log(`👤 点击作者，跳转到用户主页: ${authorId}`);
    router.push(`/user/profile/${authorId}`);
  } else {
    console.warn("❌ 作者ID不存在，无法跳转");
  }
};

const handleAvatarError = (event: Event) => {
  const img = event.target as HTMLImageElement;
  // 防止无限循环加载，只尝试一次默认图片
  if (!img.dataset.errorHandled) {
    img.dataset.errorHandled = "true";
    // 使用 base64 编码的默认头像或隐藏图片
    img.src =
      "";
  } else {
    // 如果默认图片也加载失败，隐藏图片或显示占位符
    img.style.display = "none";
  }
};
</script>

<style scoped lang="scss">
@use "@/styles/user-theme.scss" as *;

// 移动端适配
@include mobile {
  .article-header {
    padding-bottom: 16px;
    margin-bottom: 16px;
  }

  .article-title {
    @include mobile-text;

    margin-bottom: 10px;
    font-size: 18px;
    font-weight: 600;
    line-height: 1.3;
  }

  .article-meta {
    flex-direction: column;
    gap: 12px;
    align-items: flex-start;
  }

  .author-info {
    gap: 8px;

    .author-avatar {
      width: 32px;
      height: 32px;
    }

    .author-details {
      gap: 2px;

      .author-name {
        font-size: 12px;
      }

      .publish-info {
        font-size: 10px;
      }
    }
  }

  .action-buttons {
    gap: 6px;
    justify-content: flex-end;
    width: 100%;

    .el-button {
      @include mobile-button;

      min-height: 36px;
      padding: 6px 10px;
      font-size: 12px;
    }
  }

  .article-tags {
    margin-bottom: 12px;

    .article-tag {
      padding: 2px 6px;
      margin-right: 6px;
      margin-bottom: 6px;
      font-size: 11px;
    }
  }

  .article-stats {
    flex-direction: column;
    gap: 12px;
    align-items: flex-start;
    margin-bottom: 12px;
  }

  .stats-left {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    width: 100%;

    .stat-item {
      font-size: 12px;

      .el-icon {
        font-size: 13px;
      }
    }
  }

  .stats-right {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    width: 100%;

    .el-button {
      @include mobile-button;

      min-height: 32px;
      padding: 4px 8px;
      font-size: 12px;
      border-radius: 16px;

      .el-icon {
        font-size: 14px;
      }

      &.liked {
        color: #ff4757;
        background: #fff5f5;
        border-color: #ffcdd2;
      }

      &.collected {
        color: #ffa726;
        background: #fff8e1;
        border-color: #ffe0b2;
      }
    }
  }

  .article-summary {
    @include mobile-text;

    padding: 14px 12px 12px;
    margin-bottom: 12px;
    font-size: 14px;
    line-height: 1.5;
    border-radius: 8px;

    &::before {
      top: -6px;
      left: 12px;
      padding: 3px 6px;
      font-size: 11px;
    }
  }
}

@include mobile-xs {
  .article-title {
    font-size: 16px;
  }

  .author-info .author-avatar {
    width: 28px;
    height: 28px;
  }

  .action-buttons .el-button {
    min-height: 32px;
    padding: 4px 8px;
    font-size: 11px;
  }

  .stats-right .el-button {
    min-height: 28px;
    padding: 3px 6px;
    font-size: 10px;
  }
}

// iPhone XR 专用优化
@include iphone-xr {
  .stats-right .el-button {
    min-width: 44px;
    min-height: 44px; // iOS推荐的最小触摸目标
  }
}

.article-header {
  padding-bottom: 24px;
  margin-bottom: 24px;
  border-bottom: 1px solid #e4e7ed;
}

.article-title {
  margin: 0 0 16px;
  font-size: 28px;
  font-weight: 600;
  line-height: 1.4;
  color: #303133;
}

.article-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.author-info {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 8px 12px;
  margin: -8px -12px;
  cursor: pointer;
  border-radius: 8px;
  transition: all 0.3s ease;

  &:hover {
    background: var(--el-fill-color-light);

    .author-name {
      color: var(--el-color-primary);
    }
  }
}

.author-avatar {
  width: 40px;
  height: 40px;
  object-fit: cover;
  border-radius: 50%;
}

.author-details {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.author-name {
  font-size: 14px;
  font-weight: 500;
  color: #303133;
  transition: color 0.3s ease;
}

.publish-info {
  font-size: 12px;
  color: #909399;
}

.action-buttons {
  display: flex;
  gap: 8px;
}

.article-tags {
  margin-bottom: 16px;
}

.article-tag {
  margin-right: 8px;
  margin-bottom: 4px;
}

.article-stats {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.stats-left {
  display: flex;
  gap: 16px;
}

.stat-item {
  display: flex;
  gap: 4px;
  align-items: center;
  font-size: 14px;
  color: #606266;
}

.stats-right {
  display: flex;
  gap: 8px;
}

.article-summary {
  position: relative;
  padding: 20px;
  margin-bottom: 8px;
  font-size: 16px;
  line-height: 1.7;
  color: #5a5e66;
  background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
  border: 1px solid #e2e8f0;
  border-left: 4px solid #409eff;
  border-radius: 12px;
  box-shadow: 0 1px 3px rgb(0 0 0 / 4%);

  &::before {
    position: absolute;
    top: -8px;
    left: 16px;
    padding: 4px 8px;
    font-size: 12px;
    font-weight: 500;
    color: white;
    content: "摘要";
    background: #409eff;
    border-radius: 4px;
  }
}
</style>
